home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 106_01.zip / XTRINSLB.MAC < prev    next >
Text File  |  1993-06-26  |  7KB  |  376 lines

  1.     title    Xtract field from and insert field into buffer
  2.     name    ('XTRINS')
  3.     ;
  4.     ; This is intended for use with Microsoft 'M80' and 'L80'
  5.     ;
  6.     .xlist    ; turn off listing
  7.     .xcref    ; turn off cross reference label 'reference' accumulation
  8.     ;
  9.     ;
  10. @CHK    macro    ?DD    ;; Used for checking range of 8-bit displacements
  11.     if (?DD gt 7FH) and (?DD lt 0FF80H)
  12.       'Displacement Range Error - TDL LIB'    ;;force an error
  13.     endif
  14.     endm
  15.     ;
  16. LDED    macro    ?NNNN    
  17.       db    0EDH,5BH
  18.       dw    ?NNNN
  19.     endm
  20.     ;
  21. LBCD    macro    ?NNNN    
  22.       db    0EDH,4BH
  23.       dw    ?NNNN
  24.     endm
  25.     ;
  26.     ;
  27. BIT    macro    ?N,?R    
  28.       db    0CBH,?N*8+?R+40H
  29.     endm
  30.     ;
  31. SETB    macro    ?N,?R
  32.       db    0CBH,?N*8+?R+0C0H
  33.     endm
  34.     ;
  35. RES    macro    ?N,?R
  36.       db    0CBH,?N*8+?R+80H
  37.     endm
  38.     ;
  39.     ;
  40. JMPR    macro    ?N
  41.       db    18H,?N-$-1
  42.     endm
  43.     ;
  44. JRC    macro    ?N
  45.       db    38H,?N-$-1
  46.     endm
  47.     ;
  48. JRNC    macro    ?N
  49.       db    30H,?N-$-1
  50.     endm
  51.     ;
  52. JRZ    macro    ?N
  53.       db    28H,?N-$-1
  54.     endm
  55.     ;
  56. JRNZ    macro    ?N
  57.       db    20H,?N-$-1
  58.     endm
  59.     ;
  60. DJNZ    macro    ?N
  61.       db    10H,?N-$-1
  62.     endm
  63.     ;
  64.     ;
  65. RLCR    macro    ?R    
  66.       db    0CBH, 00H + ?R
  67.     endm
  68.     ;
  69. RALR    macro    ?R    
  70.       db    0CBH, 10H+?R
  71.     endm
  72.     ;
  73. RRCR    macro    ?R    
  74.       db    0CBH, 08H + ?R
  75.     endm
  76.     ;
  77. RARR    macro    ?R    
  78.       db    0CBH, 18H + ?R
  79.     endm
  80.     ;
  81. SLAR    macro    ?R    
  82.       db    0CBH, 20H + ?R
  83.     endm
  84.     ;
  85. SRAR    macro    ?R    
  86.       db    0CBH, 28H+?R
  87.     endm
  88.     ;
  89. SRLR    macro    ?R    
  90.       db    0CBH, 38H + ?R
  91.     endm
  92.     ;
  93.     ;
  94.     ;
  95.     .cref    ; re-enable cross reference 
  96.     .list    ; re-enable listing
  97.     ;
  98.     ;
  99.     ;++ **************************************************
  100.     ;
  101.     ; TO USE THIS ROUTINE:
  102.     ;
  103.     ;    M80 XTRINSLB=XTRINSLB        ...Microsoft 'M80'
  104.     ;    L80 XTRINSLB,XTRINSLB/N/E    ...Microsoft 'L80'
  105.     ;    REN BIOS.CRL=BIOS.COM
  106.     ;
  107.     ;    XTRINSLB.CRL WILL BE A 'C' COMPATIBLE RELOCATABLE FILE
  108.     ;    WHICH CAN BE REQUESTED AT CLINK OR INTEGRATED
  109.     ;    INTO YOUR LIBRARY WITH CLIB
  110.     ;
  111.     ;-- **************************************************
  112.     ;
  113. MAGIC    equ    3F7H        ;'C' PARAMETER PASSING BUFFER
  114.     ;
  115.     aseg
  116.     org    100H
  117.     .phase    0
  118.     ;
  119. ZERO:    dc    'INSERT'    ; Name of insert function
  120.     dw    INSHEAD        ; Location of insert function
  121.     dc    'XTRACT'    ; Name of xtract function
  122.     dw    XTRHEAD        ; Location of xtract function
  123.     ;
  124.     db    80H        ;END OF DIRECTORY ENTRIES
  125.     dw    FINIS - 100H    ;POINTER TO NEXT AVAIL LOCATION IN FILE
  126.     ds    (512 -($-ZERO))    ;PAD REST OF DIRECTORY
  127.     ds    5        ;RESERVED FOR 'CLIB'
  128.     ;
  129.     .dephase
  130.     ;
  131.     page
  132.     ;
  133.     ;++ ****************************************
  134.     ;
  135.     ;$$ INSERT    -- Insert bit field into a byte array
  136.     ;
  137.     ;    Bits are numbered 1..N
  138.     ;    Width may be 1..16
  139.     ;
  140.     ;    Storage format is:
  141.     ;
  142.     ;           msb    lsb
  143.     ;
  144.     ;        8      1    first byte of array
  145.     ;           16      9
  146.     ;        .      .
  147.     ;        .      .
  148.     ;        N      N-8    last byte of array
  149.     ;
  150.     ; insert(array,data,start,width)
  151.     ;    char *array;
  152.     ;    unsigned data;
  153.     ;    char start,width;
  154.     ;    {
  155.     ;      .
  156.     ;      .
  157.     ;      return 0;
  158.     ;    }
  159.     ;
  160.     ;
  161.     ; Warning:    array must be at least
  162.     ;        (start+width)/8 bytes long
  163.     ;
  164.     ;-- ****************************************
  165.     ;
  166.     .phase    $-100H
  167.     ;
  168. INSHEAD:
  169.     db    0    ; No external functions used
  170.     dw    INSTOP-INSERT    ; length of function INSERT
  171.     ;
  172.     .dephase
  173.     ;
  174.     ; Body of function INSERT
  175.     ;
  176.     .phase    0
  177.     ;
  178. INSERT:    LHLD    MAGIC    ; HL = .(byte array)
  179.     LDED    MAGIC+2    ; DE = data to be inserted
  180.     LBCD    MAGIC+4    ;  C = starting bit number
  181.     LDA    MAGIC+6
  182.     MOV    B,A    ;  B = field width in bits
  183.     DCR    C    ; bit no. 1..256 -> 0..255
  184.     PUSH    H    ; save pointer to array
  185.     LXI    H,1    ; calculate data mask
  186. MSLUP:    DAD    H
  187.     DJNZ    MSLUP
  188.     DCX    H    ; HL = 2**width -1
  189.     MOV    A,E
  190.     ANA    L    ; mask the data to be inserted
  191.     MOV    E,A
  192.     MOV    A,D
  193.     ANA    H
  194.     MOV    D,A
  195.     MOV    A,H    ; invert the mask so it can force zeroes
  196.     CMA
  197.     MOV    H,A
  198.     MOV    A,L
  199.     CMA
  200.     MOV    L,A
  201.     XTHL        ; HL = .(array), stack = inverted mask
  202.     MOV    A,C    ; calc byte offset
  203.     ANI    not 7
  204.     RRC
  205.     RRC
  206.     RRC
  207.     ADD    L
  208.     MOV    L,A
  209.     JRNC    ADOK
  210.     INR    H    ; HL = .(three bytes of interest)
  211. ADOK:    MOV    A,C    ; calc bit in byte i.e. bitno. mod 8
  212.     ANI    7
  213.     INR    A    ; 0..7 -> 1..8
  214.     MOV    C,A    ; save for right justify shift
  215.     MOV    B,A    ; again for left justify shift
  216.     XCHG        ; DE = .(bytes of interest)
  217.     XTHL        ;stack = data to be inserted, HL = inverted mask
  218.     PUSH    H    ; stack = inverted mask
  219.     INX    D    ; get 3 byte of interest in H'L'A
  220.     INX    D
  221.     LDAX    D
  222.     MOV    H,A
  223.     DCX    D
  224.     LDAX    D
  225.     MOV    L,A
  226.     DCX    D
  227.     LDAX    D
  228. RJLUP:    ORA    A
  229.     DCR    C
  230.     JRZ    RJDUN    ; Is right justification complete ?
  231.     RARR    H    ; No
  232.     RARR    L
  233.     RAR
  234.     JRNC    RJLUP
  235.     SETB    7,H
  236.     JMPR    RJLUP
  237.     ;
  238. RJDUN:    XCHG        ; HL = .(bytes of interest), DE = rj data
  239.     XTHL        ; HL = inverted mask, stack = .(bytes o i)
  240.     ANA    L
  241.     MOV    L,A
  242.     MOV    A,E
  243.     ANA    H
  244.     MOV    E,A
  245.     MOV    A,L    ; D'E'A = rj field masked to zeroes
  246.     POP    H
  247.     XTHL        ; HL = data to be inserted, stack = .(bytes o i)
  248.     ORA    L    ; or in the data to be inserted
  249.     MOV    L,A
  250.     MOV    A,E
  251.     ORA    H
  252.     MOV    E,A
  253.     MOV    A,L
  254.             ;B = shift count to re-justify as was originally
  255. LJLUP:    ORA    A
  256.     DCR    B
  257.     JRZ    LJDUN    ; Is left justify complete ?
  258.     RAL        ; No
  259.     RALR    E
  260.     RALR    D
  261.     JRNC    LJLUP
  262.     SETB    0,A
  263.     JMPR    LJLUP
  264.     ;
  265. LJDUN:    POP    H    ; Hl = .(bytes of interest)
  266.     MOV    M,A
  267.     INX    H
  268.     MOV    M,E
  269.     INX    H
  270.     MOV    M,D    ; modified bytes of interest restored
  271.     LXI    H,0    ; Function value returned
  272.     RET        ; and exit
  273. INSTOP:
  274.     dw    0    ; No non-intrinsic relocatable references
  275.     ;
  276.     .dephase
  277.     ;
  278.     page
  279.     ;
  280.     ;++ ****************************************
  281.     ;
  282.     ;$$ EXTRACT -- Extract a bit field from a
  283.     ;        -- multi-byte buffer
  284.     ;
  285.     ;    Bits are numbered 1..N
  286.     ;    Width may be 1..16
  287.     ;
  288.     ;    Storage format is presumed to be:
  289.     ;
  290.     ;           msb    lsb
  291.     ;
  292.     ;        8      1    first byte of array
  293.     ;           16      9
  294.     ;        .      .
  295.     ;        .      .
  296.     ;        N      N-8    last byte of array
  297.     ;
  298.     ;
  299.     ; xtract(array,start,width)
  300.     ;    char *array;
  301.     ;    char start,width;
  302.     ;    {
  303.     ;    .
  304.     ;    .
  305.     ;    return <extracted value>;
  306.     ;    }
  307.     ;
  308.     ;
  309.     ;-- ****************************************
  310.     ;
  311.     .phase    $-100H
  312.     ;
  313. XTRHEAD:
  314.     db    0
  315.     dw    XTRTOP-XTRACT
  316.     ;
  317.     .dephase
  318.     ;
  319.     .phase    0
  320.     ;
  321. XTRACT:    LHLD    MAGIC    ; HL =.(byte array)
  322.     LBCD    MAGIC+2    ;  C = starting bit number
  323.     LDA    MAGIC+4    
  324.     MOV    B,A    ;  B = field width in bits
  325.     DCR    C    ; bit no.  1..N -> 0..(N-1)
  326.     MOV    A,C
  327.     ANI    not 7
  328.     RRC
  329.     RRC
  330.     RRC
  331.     MOV    E,A
  332.     MVI    D,0    ; DE = byte offset
  333.     DAD    D    ; HL = .(bytes containing bits of interest)
  334.     MOV    A,C
  335.     ANI    7
  336.     MOV    C,A    ; C = bit number mod bytesize (i.e. 8)
  337.     INR    C    ; 0..7 -> 1..8
  338.     XCHG        ; DE = .(bytes of interest)
  339.     INX    D
  340.     INX    D    ; Could be spread across 3 bytes
  341.     LDAX    D    ; get them in H'L'A
  342.     MOV    H,A
  343.     DCX    D
  344.     LDAX    D
  345.     MOV    L,A
  346.     DCX    D
  347.     LDAX    D
  348. LOOP:    DCR    C    ; Right justify in H'L'A
  349.     JRZ    JUSTDN    ; Is right justification complete ?
  350.     SRAR    H    ; No
  351.     RARR    L
  352.     RAR
  353.     JMPR    LOOP
  354.     ;
  355. JUSTDN:    MOV    D,L
  356.     MOV    E,A    ; max 16 bit field right justified in D'E
  357.     LXI    H,1    ; Calculate the field width mask
  358. MSKLUP:    DAD    H
  359.     DJNZ    MSKLUP
  360.     DCX    H    ; HL = 2**width -1
  361.     MOV    A,D
  362.     ANA    H
  363.     MOV    H,A
  364.     MOV    A,E
  365.     ANA    L
  366.     MOV    L,A    ; HL = extracted value
  367.     RET
  368.     ;
  369. XTRTOP:
  370.     dw    0
  371.     ;
  372.     .dephase
  373.     ;
  374.     ;
  375. FINIS:    end    ZERO
  376.